package com.dcy.daily.common.exception;


import com.dcy.daily.common.utils.I18nUtil;
import com.dcy.daily.common.utils.StringUtils;

/**
 * 基础异常
 * 
 */
public class BaseException extends RuntimeException
{
    private static final long serialVersionUID = 1L;

    /**
     * 所属模块
     */
    private String module;

    /**
     * 首位前端错误码，末位后台错误码
     */
    private String[] codes;

    /**
     * 错误码
     */
    private String code;

    /**
     * 错误码对应的参数
     */
    private Object[] args;

    /**
     * 自定义错误信息，会记录在日志中
     */
    private String errMSg;

    /**
     * 国际化信息,默认为前端错误域信息
     */
    private String message;

    private boolean printExcep = true;

    public BaseException(int module, String code, Object[] args, String errMSg, Throwable cause)
    {
        super(cause);
        this.module = null;
        this.code = code;
        this.args = args;
        this.errMSg = StringUtils.isNotEmpty(errMSg)?errMSg:"";
        if (StringUtils.isNotEmpty(code)) {
            message = I18nUtil.message(code, args);
        }
        if(StringUtils.isNull(cause)) {
            printExcep = false;
        }
    }

    public BaseException(int module, String code, String errMSg, Throwable cause)
    {
        this(module, code, null, errMSg, cause);
    }

    public BaseException(int module, String code, Object[] args, String errMSg)
    {
        this(module, code, args, errMSg, null);
    }

    public BaseException(int module, String code, Object[] args, Throwable cause)
    {
        this(module, code, args, null, cause);
    }

    public BaseException(int module, String code, Object[] args)
    {
        this(module, code, args, null, null);
    }

    public BaseException(int module, String code, Throwable cause)
    {
        this(module, code, null, null, cause);
    }

    public BaseException(int module, String code, String errMSg)
    {
        this(module, code, null, errMSg, null);
    }

    public BaseException(int module, String code)
    {
        this(module, code, null, null, null);
    }

    public BaseException(String code, Object[] args, String errMSg, Throwable cause)
    {
        this(-1, code, args, errMSg, cause);
    }

    public BaseException(String code, Object[] args, String errMSg)
    {
        this(-1, code, args, errMSg, null);
    }

    public BaseException(String code, String errMSg, Throwable cause)
    {
        this(-1, code, null, errMSg, cause);
    }

    public BaseException(String code, String errMSg)
    {
        this(-1, code, null, errMSg, null);
    }

    public BaseException(String code, Object[] args, Throwable cause)
    {
        this(-1, code, args, null, cause);
    }

    public BaseException(String code, Object[] args)
    {
        this(-1, code, args, null, null);
    }

    public BaseException(String code, Throwable cause)
    {
        this(-1, code, null, null, cause);
    }

    public BaseException(String code)
    {
        this(-1, code, null, null, null);
    }

    public BaseException(int module, String[] codes, Object[] args, String errMSg, Throwable cause)
    {
        super(cause);
        this.module = null;
        this.codes = codes;
        this.args = args;
        this.errMSg = StringUtils.isNotEmpty(errMSg)?errMSg:"";
        if(StringUtils.isNotNull(codes)) {
            code = codes[0];
        }
        if (StringUtils.isNotEmpty(code)) {
            message = I18nUtil.message(code, args);
        }
        if(StringUtils.isNull(cause)) {
            printExcep = false;
        }
    }

    public BaseException(int module, String[] codes, String errMSg, Throwable cause)
    {
        this(module, codes, null, errMSg, cause);
    }

    public BaseException(int module, String[] codes, Object[] args, String errMSg)
    {
        this(module, codes, args, errMSg, null);
    }

    public BaseException(int module, String[] codes, Object[] args, Throwable cause)
    {
        this(module, codes, args, null, cause);
    }

    public BaseException(int module, String[] codes, Object[] args)
    {
        this(module, codes, args, null, null);
    }

    public BaseException(int module, String[] codes, Throwable cause)
    {
        this(module, codes, null, null, cause);
    }

    public BaseException(int module, String[] codes, String errMSg)
    {
        this(module, codes, null, errMSg, null);
    }

    public BaseException(int module, String[] codes)
    {
        this(module, codes, null, null, null);
    }

    public BaseException(int module, Throwable cause)
    {
        this(module, "", null, null, cause);
    }

    public BaseException(String[] codes, Object[] args, String errMSg, Throwable cause)
    {
        this(-1, codes, args, errMSg, cause);
    }

    public BaseException(String[] codes, Object[] args, String errMSg)
    {
        this(-1, codes, args, errMSg, null);
    }

    public BaseException(String[] codes, String errMSg, Throwable cause)
    {
        this(-1, codes, null, errMSg, cause);
    }

    public BaseException(String[] codes, String errMSg)
    {
        this(-1, codes, null, errMSg, null);
    }

    public BaseException(String[] codes, Object[] args, Throwable cause)
    {
        this(-1, codes, args, null, cause);
    }

    public BaseException(String[] codes, Object[] args)
    {
        this(-1, codes, args, null, null);
    }

    public BaseException(String[] codes, Throwable cause)
    {
        this(-1, codes, null, null, cause);
    }

    public BaseException(String[] codes)
    {
        this(-1, codes, null, null, null);
    }


    @Override
    public String getMessage()
    {
        return message;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public String getErrMSg() {
        return errMSg;
    }

    public void setErrMSg(String errMSg) {
        this.errMSg = errMSg;
    }

    public String[] getCodes() {
        return codes;
    }

    public void setCodes(String[] codes) {
        this.codes = codes;
    }

    public boolean isPrintExcep() {
        return printExcep;
    }

    public void setPrintExcep(boolean printExcep) {
        this.printExcep = printExcep;
    }
}
